{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "2.3.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/pytorch/ch2/2_3.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6Sn_rH5RNADz",
        "colab_type": "text"
      },
      "source": [
        "## 2.3 自动求梯度"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gzqfy4-RN1ez",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "fda61f6c-72ad-4c3e-a2f5-717f3c29a74d"
      },
      "source": [
        "import torch\n",
        "\n",
        "x = torch.ones(2, 2, requires_grad=True)\n",
        "x "
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1.],\n",
              "        [1., 1.]], requires_grad=True)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iULXXyovN3XT",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "bb736fc4-56e0-4313-8cf5-30ae52d102be"
      },
      "source": [
        "print(x.grad_fn)"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "None\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8RoLpzjwOAM7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "b2959c3a-5497-4544-de19-4a28d1c540d5"
      },
      "source": [
        "y = x + 2 \n",
        "y"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[3., 3.],\n",
              "        [3., 3.]], grad_fn=<AddBackward0>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7D9zr1hdOVQ4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "e08f124e-e29b-4c42-fff8-a41e9caf183d"
      },
      "source": [
        "y.grad_fn"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<AddBackward0 at 0x7f5207e63470>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZJDObc9fOfrQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4a37cea5-1ed2-4699-f534-8e2e51c13bdb"
      },
      "source": [
        "x.is_leaf, y.is_leaf"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(True, False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dPBWWVDnO5Ou",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# x.is_leaf?\n",
        "\n",
        "# requires_grad 是 False 的时候，是 leaf。"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m-d_izleO_fW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "6266824d-7ad8-44f8-c582-62ec0dc701f5"
      },
      "source": [
        "z = y * y * 3 \n",
        "z"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[27., 27.],\n",
              "        [27., 27.]], grad_fn=<MulBackward0>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6oev2PtVQD8J",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "7facf496-25eb-49e7-9528-c52f2ed4000d"
      },
      "source": [
        "out = z.mean()\n",
        "out"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(27., grad_fn=<MeanBackward0>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EMKcU67mQH-8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "08222328-f289-4c44-c1b9-b5b5de087fcc"
      },
      "source": [
        "a = torch.randn(2, 2)\n",
        "a = ((a * 3) / (a - 1))\n",
        "a.requires_grad"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3eX7ok4ZWtKg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c369e832-5f26-43e6-d01b-74a26d525c78"
      },
      "source": [
        "a.requires_grad_(True)\n",
        "a.requires_grad"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5XmBtU79W04F",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "0a663e42-328b-4b2f-b963-336cb16771ae"
      },
      "source": [
        "b = (a * a).sum()\n",
        "b.grad_fn"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<SumBackward0 at 0x7f51be321d68>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ssy9lLURW-I1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "ce6a477d-d07e-43b1-ee84-1e204a3513ed"
      },
      "source": [
        "out.backward()\n",
        "x.grad"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[4.5000, 4.5000],\n",
              "        [4.5000, 4.5000]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WQ-qAYy2XCcE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "3afec314-2903-441a-865f-f608b8090927"
      },
      "source": [
        "out2 = x.sum()\n",
        "out2.backward()\n",
        "x.grad"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[5.5000, 5.5000],\n",
              "        [5.5000, 5.5000]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q8kbC6ssXIKT",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "0e60ace2-6606-45cf-c6a7-d0e787d7421b"
      },
      "source": [
        "out3 = x.sum()\n",
        "x.grad.data.zero_()\n",
        "out3.backward()\n",
        "x.grad"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1.],\n",
              "        [1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-d9dBD3oXYdL",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "7dc2413e-efb1-4a35-8c53-a2e2507dce72"
      },
      "source": [
        "x = torch.tensor([1.0, 2.0, 3.0, 4.0], requires_grad=True)\n",
        "y = 2 * x \n",
        "z = y.view(2, 2)\n",
        "z"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[2., 4.],\n",
              "        [6., 8.]], grad_fn=<ViewBackward>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rOcvU4bcXk8S",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "e634771b-b25f-4586-93f9-ae175291007b"
      },
      "source": [
        "v = torch.tensor([[1.0, 0.1], [0.01, 0.001]], dtype=torch.float)\n",
        "z.backward(v)\n",
        "x.grad"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([2.0000, 0.2000, 0.0200, 0.0020])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aLpYNvTlYQjw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "7a7a3535-efb0-43ce-8624-797079fdb71b"
      },
      "source": [
        "x = torch.tensor(1.0, requires_grad=True)\n",
        "y1 = x ** 2 \n",
        "with torch.no_grad():\n",
        "    y2 = x ** 3 \n",
        "y3 = y1 + y2 \n",
        "\n",
        "print(x, x.requires_grad)\n",
        "print(y1, y1.requires_grad)\n",
        "print(y2, y2.requires_grad)\n",
        "print(y3, y3.requires_grad)"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(1., requires_grad=True) True\n",
            "tensor(1., grad_fn=<PowBackward0>) True\n",
            "tensor(1.) False\n",
            "tensor(2., grad_fn=<AddBackward0>) True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YxS1D_gvYvlm",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b5b98f51-fc66-420b-c615-8660de669808"
      },
      "source": [
        "y3.backward()\n",
        "x.grad"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor(2.)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4JHSV0ueY0nY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "c5af5be4-1796-49d5-e0e1-4e3cb089e403"
      },
      "source": [
        "x = torch.ones(1, requires_grad=True)\n",
        "print(x.data)\n",
        "print(x.data.requires_grad)\n",
        "\n",
        "y = 2 * x \n",
        "x.data *= 100 \n",
        "y.backward()\n",
        "print(x)\n",
        "print(x.grad)"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1.])\n",
            "False\n",
            "tensor([100.], requires_grad=True)\n",
            "tensor([2.])\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}
